<template>
  <div>
    <transition name="appear">
      <div class="settingBg" v-if="settingBoxFlag" :class="{settingBgDark:darkFlag}">
        <!-- 设置页面主体 -->
        <div
          class="settingBox"
          :class="{ settingBoxDark:  darkFlag }"
        >
          <i class="el-icon-close" @click="closeSettingBox()"></i>
          <p class="title"  :class="{ sTitleDark:  darkFlag }">设置</p>
          <!-- 登录设置 -->
          <p class="sTitle" :class="{ sTitleDark:  darkFlag }">账户设置</p>
          <div
            class="smallSetBox"
            style="height: 90px"
            :class="{ smallSetBoxDark:  darkFlag }"
            v-if="!loginSuccess"
          >
            <button class="loginBtn" @click="loginBoxOn()">登录/注册</button>
            <p class="smallSetP">登录即可同步个性设置</p>
            <p class="smallSetP" style="padding-top: 10px">
              同时将自定义搜索、个性图标以及备忘录上传云端保存
            </p>
          </div>
          <div
            class="smallSetBox"
            style="height: 85px"
            :class="{ smallSetBoxDark:  darkFlag }"
            v-if="loginSuccess"
          >
            <p
              style="
                float: left;
                margin-left: 20px;
                margin-top: 25px;
                font-size: 20px;
              "
            >
              账号：{{ userName }}
            </p>
            <button class="loginBtn" style="float: right; margin-top: 15px" @click="changeBoxOn()">
              修改账号信息
            </button>
          </div>
          <!-- 主题设置 -->
          <p class="sTitle"  :class="{ sTitleDark: darkFlag }">主题设置</p>
          <div
            class="smallSetBox"
            style="height: 150px"
            :class="{ smallSetBoxDark:  darkFlag }"
          >
            <!-- 一行一条设置 -->
            <div class="oneLineBox">
              <p class="oneLineP">夜间模式</p>
              <el-switch
                v-model="darkFlag"
                active-color="rgb(253, 180, 22)"
                class="setSwitch"
              ></el-switch>
            </div>
            <div class="oneLineBox">
              <p class="oneLineP">18:00后自动切换夜间模式</p>
              <el-switch
                v-model="timeToDarkFlag"
                active-color="rgb(253, 180, 22)"
                class="setSwitch"
              ></el-switch>
            </div>
            <div class="oneLineBox">
              <p class="oneLineP">切换壁纸</p>
              <button class="setSmallBtn" @click="bgChangeOn()">
                点击切换壁纸
              </button> 
            </div>
          </div>
          <!-- 搜索设置 -->
          <p class="sTitle"  :class="{ sTitleDark:  darkFlag }">搜索设置</p>
          <div
            class="smallSetBox"
            style="height: 150px"
            :class="{ smallSetBoxDark:  darkFlag }"
          >
            <div class="oneLineBox">
              <p class="oneLineP">提示：使用Alt+数字键可以快捷切换搜索引擎</p>
            </div>
            <div class="oneLineBox">
              <p class="oneLineP">自动清空搜索框</p>
              <el-switch
                v-model="inputClearFlag"
                active-color="rgb(253, 180, 22)"
                class="setSwitch"
                v-bind="inputClear()"
              ></el-switch>
            </div>
            <div class="oneLineBox">
              <p class="oneLineP">切换搜索引擎</p>
              <button class="setSmallBtn" @click="enginChangeOn()">
                点击切换
              </button>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import bus from "../bus";
export default {
  data() {
    return {
      settingBoxFlag: false,
      setDarkFlag: true,
      timeToDarkFlag: false,
      inputClearFlag: false,
      // 登录
      userName: "",
      loginSuccess: false,
    };
  },
  mounted() {
    bus.$on("settingOnFlag", (flag) => {
      this.settingBoxFlag = flag;
    });
    // 接收用户名
    bus.$on("userNameFlag", (userName,flag) => {
      this.userName = userName;
      this.loginSuccess = flag;
    });
    let locid = JSON.parse(
        window.localStorage.getItem("locid")
    );
    let locname = JSON.parse(
        window.localStorage.getItem("locname")
    );
    if(locid){
      this.userid=locid;
      this.userName=locname;
      this.loginSuccess=true;
    }
  },
  beforeDestroy() {
    bus.$off();
    clearInterval(darkTimer);
  },
  methods: {
    // 关闭设置
    closeSettingBox() {
      this.settingBoxFlag = false;
    },
    // 打开壁纸弹窗
    bgChangeOn() {
      this.settingBoxFlag = false;
      bus.$emit("bgFlag2", true);
    },
    // 打开搜索引擎切换列表
    enginChangeOn() {
      this.settingBoxFlag = false;
      bus.$emit("enginChangeFlag", true);
    },
    // 自动清空搜索框
    inputClear() {
      if (this.inputClearFlag) {
        bus.$emit("enginFlag", true);
      } else {
        bus.$emit("enginFlag", false);
      }
    },
    // 登录页面弹窗
    loginBoxOn() {
      this.settingBoxFlag = false;
      bus.$emit("loginOnFlag", true);
    },
    // 修改页面弹窗
    changeBoxOn() {
      this.settingBoxFlag = false;
      bus.$emit("changeOnFlag", true);
    },
    timeToDark(){
      let h = new Date().getHours();
      if (((h >= 18) & (h <= 24)) | ((h >= 0) & (h <= 6))) {
        this.$store.state.darkFlag = true
      }
    }
  },
  computed:{
    darkFlag:{
      get(){
        return this.$store.state.darkFlag
      },
      set(){
        this.$store.dispatch('darkModel',this.darkFlag)
        return this.$store.state.darkFlag
      }
    }
  },
  watch: {
    // 18:00后自动切换夜间模式
    timeToDarkFlag: {
      handler() {
        if (this.timeToDarkFlag) {
          this.timeToDark()
          var darkTimer = setInterval(() => this.timeToDark(), 30000);
        }else{
          clearInterval(darkTimer);
        }
      },
    },
  },
};
</script>

<style>
.settingBg {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.15);
  z-index: 105;
  transition: all .2s;
  display: flex;
  justify-content: center;
  align-items: center;
}
.settingBgDark {
  background-color: rgba(0, 0, 0, 0.2);
}
/* 设置页面主体 */
.settingBox {
  position: relative;
  width: 650px;
  height: 550px;
  background-color: rgb(243, 243, 243);
  z-index: 110;
  border-radius: 20px;
  box-shadow: 0 0 4px 5px rgba(168, 168, 168, 0.3);
  text-align: center;
  color: black;
  transition: all 0.3s;
  overflow-x: hidden;
  overflow-y: scroll;
}
.settingBox i {
  position: absolute;
  top: 15px;
  right: 15px;
  font-size: 26px;
  transition: all 0.2s;
  z-index: 120;
  cursor: pointer;
}
.settingBox i:hover {
  color: red;
}
.settingBox .title {
  font-size: 30px;
  margin-top: 20px;
  font-weight: bold;
  color: #515151;
}
/* 深色模式 */
.settingBoxDark {
  background-color: rgb(33, 33, 33);
  color: #fff;
}
.settingBoxDark .title{
  color: #fff;
}
/* 分类板块 */
.sTitle {
  text-align: left;
  font-size: 24px;
  margin-left: 55px;
  margin-top: 30px;
  color: #515151;
}
.sTitleDark {
  color: #ffffff;
}
.smallSetBox {
  width: 570px;
  margin: 40px;
  margin-top: 10px;
  background-color: #fff;
  text-align: left;
  border-radius: 10px;
  font-size: 17px;
}
.smallSetP {
  width: 580px;
  padding-top: 20px;
  margin-left: 20px;
  color: #515151;
}
/* 夜间模式 */
.smallSetBoxDark {
  background-color: rgb(48, 48, 48);
  color: #fff;
}
.smallSetBoxDark .smallSetP{
  color: #fff;
}
/* 登录设置 */
.loginBtn {
  width: 100px;
  height: 50px;
  background-color: #fdaf27;
  margin: 20px;
  border-radius: 10px;
  cursor: pointer;
  float: right;
  transition: all 0.2s;
  color: #2d2d2d;
}
.loginBtn:hover {
  background-color: rgb(255, 174, 0);
}
/* 单行设置 */
.oneLineBox {
  width: 100%;
  height: 30px;
  padding-top: 15px;
}
.oneLineP {
  margin-top: 5px;
  text-align: left;
  float: left;
  margin-left: 20px;
}
.setSwitch {
  float: right;
  margin-top: 5px;
  margin-right: 25px;
}
.setSmallBtn {
  float: right;
  width: 100px;
  height: 30px;
  margin-right: 20px;
  border-radius: 5px;
  cursor: pointer;
  color: rgb(52, 52, 52);
  transition: all 0.2s;
}
.setSmallBtn:hover {
  background-color: #dfdfdf;
}

.appear-enter-active,
.appear-leave-active {
  transition: all 0.5s;
}
.appear-enter,
.appear-leave-to {
  opacity: 0;
}
/* 滚动条 */
.settingBox::-webkit-scrollbar {
  width: 6px;
}
.settingBox::-webkit-scrollbar-button {
  display: block;
  height: 20px;
}
.settingBox::-webkit-scrollbar-thumb {
  width: 6px;
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 6px;
}
/* .settingBoxDark::-webkit-scrollbar-thumb {
  width: 6px;
  background-color: rgba(255, 255, 255,0.3);
  border-radius: 6px;
} */
</style>